define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cash/index',
                    add_url: 'cash/add',
                    edit_url: 'cash/edit',
                    del_url: 'cash/del',
                    multi_url: 'cash/multi',
                    table: 'cash'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'money', title: __('Money')},
                        {field: 'fee', title: __('Fee')},
                        {field: 'bank', title: __('Bank')},
                        {field: 'bankCard', title: __('BankCard')},
                        {field: 'createTime', title: __('CreateTime'), formatter: Table.api.formatter.datetime},
                        {field: 'updateTime', title: __('UpdateTime'), formatter: Controller.api.formatter.updateTime},
                        {field: 'status', title: __('Status'),formatter: Controller.api.formatter.status},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {

                status:function(value , row, index){
                    if(value==1){
                        return "处理中";
                    }
                    if(value==0){
                        return "待处理";
                    }
                    if(value==2){
                        return "已提现";
                    }
                },

                updateTime:function(value , row, index){
                                    if(row.status==2){
                                        return value ? Moment(parseInt(value) * 1000).format("YYYY-MM-DD HH:mm:ss") : __('None');
                                    }else{
                                        return '';
                                    }
                                },

                operate: function (value, row, index) {
                    //返回字符串加上Table.api.formatter.operate的结果
                    //默认需要按需显示排序/编辑/删除按钮,则需要在Table.api.formatter.operate将table传入
                    //传入了table以后如果edit_url为空则不显示编辑按钮,如果del_url为空则不显显删除按钮
                    return '<a class="btn btn-info btn-xs btn-detail"><i class="fa fa-list"></i></a>'
                }
            },
            events: {
                operate: $.extend({
                    'click .btn-detail': function (e, value, row, index) {
                        Backend.api.open('cash/detail/id/' + row['id'], __('Detail'));
                    }
                }, Table.api.events.operate)
            }
        }
    };
    return Controller;
});